﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
//using Microsoft.WindowsAPICodePack.Dialogs;

namespace TestPictrueGenerator
{

    

    public partial class Form1 : Form
    {
        void coverFile(Image img, string fn)
        {
            if (System.IO.File.Exists(fn))
            {
                System.IO.File.Delete(fn);
            }
            
            
            img.Save(fn, System.Drawing.Imaging.ImageFormat.Bmp);
            
        }

        void saveImage(Image img, string fp, string fn, string ext)
        {
            string fullName = Path.Combine(fp, fn + ext);
            Console.WriteLine("full output file name: " + fullName);

            if (System.IO.File.Exists(fullName))
            {
                System.IO.File.Delete(fullName);
            }

            if(ext.Equals(".bmp"))
                img.Save(fullName, System.Drawing.Imaging.ImageFormat.Bmp);

            if (ext.Equals(".jpg"))
                img.Save(fullName, System.Drawing.Imaging.ImageFormat.Jpeg);

        }

        bool isSelectAllTestPics = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*foreach (KeyValuePair<string, int> kvp in YESLCD_Stander_Resolution.)
            {
                Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
            }
            comboBox_LCD_Resolution_YES_LCD.Items.Add("")*/
        }

        private void button_Generate_Click(object sender, EventArgs e)
        {
            #region 选择输出文件夹

            string outpuFolder;

            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            if(string.IsNullOrEmpty( Properties.Settings.Default.lastOutputFolder) == true)
            {
                dialog.InitialDirectory = "C:\\Users";
            } else
            {
                dialog.InitialDirectory = Properties.Settings.Default.lastOutputFolder;
            }
            
            dialog.IsFolderPicker = true;


            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                //MessageBox.Show("You selected: " + dialog.FileName);
                outpuFolder = dialog.FileName;

                Properties.Settings.Default.lastOutputFolder = outpuFolder;
                Properties.Settings.Default.Save();
            }
            else
            {
                return;
            }

            #endregion

            #region 获取时间, 产生时间字符串作为文件名前缀
            string fileNamePrefix = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            Console.WriteLine("file name prefix: " + fileNamePrefix);
            #endregion

            #region 文件扩展名
            string filePostFix;

            if(radioButton_BMP.Checked == true)
            {
                filePostFix = ".bmp";
            } else
            {
                filePostFix = ".jpg";
            }
            Console.WriteLine("file Post Fix = " + filePostFix);
            #endregion


            #region 获取分辨率信息
            int resX = int.Parse(textBox_ResX.Text);
            int resY = int.Parse(textBox_ResY.Text);
            Console.WriteLine(string.Format("output picture size: ({0},{1})", resX, resY));
            #endregion

            BitmapGenerator bg = new BitmapGenerator(resX, resY);

            #region YES-LCD Standard Test Images
            #region 1
            if (checkBox_Frame.Checked)
            {
                Console.WriteLine("checkBox_Frame.Checked");
                var imagedata = bg.createFrameImage();
                saveImage(imagedata, outpuFolder, "1", filePostFix);
            }
            #endregion

            #region 2
            if (checkBox_White.Checked)
            {
                var imagedata = bg.createColorImage(Color.White);
                saveImage(imagedata, outpuFolder, "2", filePostFix);
            }
            #endregion

            #region 3
            if (checkBox_Red.Checked)
            {
                var imagedata = bg.createColorImage(Color.Red);
                saveImage(imagedata, outpuFolder, "3", filePostFix);
            }
            #endregion

            #region 4
            if (checkBox_Green.Checked)
            {
                Color pureGreen = Color.FromArgb(255, 0, 255, 0);
                var imagedata = bg.createColorImage(pureGreen);
                saveImage(imagedata, outpuFolder, "4", filePostFix);
            }
            #endregion

            #region 5
            if (checkBox_BLue.Checked)
            {
                var imagedata = bg.createColorImage(Color.Blue);
                saveImage(imagedata, outpuFolder, "5", filePostFix);
            }
            #endregion

            #region 6
            if (checkBox_Black.Checked)
            {
                var imagedata = bg.createColorImage(Color.Black);
                saveImage(imagedata, outpuFolder, "6", filePostFix);
            }
            #endregion

            #region 7
            if (checkBox_Gray.Checked)
            {
                Bitmap original = (Bitmap)Properties.Resources._7;
                Bitmap resized = new Bitmap(original, new Size(resX, resY));
                saveImage(resized, outpuFolder, "7", filePostFix);
            }
            #endregion

            #region 8
            if (checkBox_Gray.Checked)
            {
                Bitmap original = (Bitmap)Properties.Resources._8;
                Bitmap resized = new Bitmap(original, new Size(resX, resY));
                saveImage(resized, outpuFolder, "8", filePostFix);
            }
            #endregion

            #region 9
            if (checkBox_Gray.Checked)
            {
                Bitmap original = (Bitmap)Properties.Resources._9;
                Bitmap resized = new Bitmap(original, new Size(resX, resY));
                saveImage(resized, outpuFolder, "9", filePostFix);
            }
            #endregion
            #endregion
            #region Scaler
            if (checkBox_Scale.Checked)
            {
                Console.WriteLine("checkBox_Scale.Checked");
                var imagedata = bg.createScaleImage();
                saveImage(imagedata, outpuFolder, "Scale", filePostFix);
            }
            #endregion

            #region Flicker
            if (checkBox_Flicker_Dot.Checked)
            {
                Console.WriteLine("checkBox_Flicker.Checked");
                var imagedata = bg.createScaleImage();
                saveImage(imagedata, outpuFolder, "Flicker_Dot", filePostFix);
            }
            #endregion

            #region 边框斜三角
            if (checkBox_Frame_Triangle.Checked)
            {
                Console.WriteLine("checkBox_Frame_Triangle.Checked");
                var imagedata = bg.createFrameTriangleImage();
                saveImage(imagedata, outpuFolder, "Frame_Triangle", filePostFix);
            }
            #endregion


            #region 完成, 询问是否打开输出文件夹
            DialogResult dr = MessageBox.Show("是否打开输出文件夹", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                try
                {
                    System.Diagnostics.Process.Start(outpuFolder);

                }
                catch
                {
                    MessageBox.Show("存储位置未设置！");
                }
            }
            else if (dr == DialogResult.Cancel)
            {
                //MessageBox.Show("您选择的是【取消】");
            }
            #endregion
        }

        private void button_allTestSwitch_Click(object sender, EventArgs e)
        {
            isSelectAllTestPics = !isSelectAllTestPics;

            if(isSelectAllTestPics)
            {
                button_allTestSwitch.Text = "Unselect All";
                checkBox_Frame.Checked = true;
                checkBox_White.Checked = true;
                checkBox_Red.Checked = true;
                checkBox_Green.Checked = true;
                checkBox_BLue.Checked = true;
                checkBox_Black.Checked = true;
                checkBox_Gray.Checked = true;
                checkBox_ColorBar.Checked = true;
                checkBox_Pic.Checked = true;
            } else
            {
                button_allTestSwitch.Text = "Select All";
                checkBox_Frame.Checked = false;
                checkBox_White.Checked = false;
                checkBox_Red.Checked = false;
                checkBox_Green.Checked = false;
                checkBox_BLue.Checked = false;
                checkBox_Black.Checked = false;
                checkBox_Gray.Checked = false;
                checkBox_ColorBar.Checked = false;
                checkBox_Pic.Checked = false;
            }
        }

        private void ToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
